<?php
/**
 * Created by PhpStorm.
 * User: kajun
 * Date: 2017/4/7
 * Time: 15:07
 */
namespace Home\Controller;

use \Home\Controller\HomeController;
use Think\Page;

class  InfoController extends HomeController
{
    public function demand()
    {
        $p = I('p', '1');
        $where['type'] = array('eq','需求方');
        $where['finish'] = array('eq','0');
        $where['show'] = array('neq','0');
        $data = D('MainInfo')->where($where)->select();
        $count = count($data);
        $page_size = 20;
        $page = new Page($count, $page_size);
        $list = D('MainInfo')->page($p . ',' . $page_size)->where($where)->order('modify_time desc')->select();
        $server = D('Server')->limit(1)->select();

        //        微博
        $weibo = M('Url')->find();
        $this->assign('weibo', $weibo);

        /** 友情链接调用**/
        $link = D('Link')->order('create_time desc')->select();
        $this->assign('link',$link);
        $advs = M('Advertisement')->where(array('show' => '1'))->order('create_time desc')->select();
        $this->assign('advs',$advs);

        $this->assign('server', $server);
        $this->assign('list', $list);
        $this->assign('page', $page->showFront());
        $this->display();
    }

    public function supply()
    {
        $p = I('p', '1');
        $where['type'] = array('eq','供应方');
        $where['finish'] = array('eq','0');
        $where['show'] = array('neq','0');
        $data = D('MainInfo')->where($where)->select();
        $count = count($data);
        $page_size = 20;
        $page = new Page($count, $page_size);
        $list = D('MainInfo')->page($p . ',' . $page_size)->where($where)->order('modify_time desc')->select();
        $server = D('Server')->limit(1)->select();
        //        微博
        $weibo = M('Url')->find();
        $this->assign('weibo', $weibo);

        /** 友情链接调用**/
        $link = D('Link')->order('create_time desc')->select();
        $this->assign('link',$link);
        $advs = M('Advertisement')->where(array('show' => '1'))->order('create_time desc')->select();
        $this->assign('advs',$advs);
        $this->assign('server', $server);
        $this->assign('list', $list);
        $this->assign('page', $page->showFront());
        $this->display();

    }

    public function deal()
    {
        $p = I('p', '1');
        $where['finish'] = array('eq','1');
        $where['show'] = array('neq','0');
        $data = D('MainInfo')->where($where)->select();
        $count = count($data);
        $page_size = 20;
        $page = new Page($count, $page_size);
        $list = D('MainInfo')->page($p . ',' . $page_size)->where($where)->order('modify_time desc')->select();
        $server = D('Server')->limit(1)->select();
        /** 友情链接调用**/
        $link = D('Link')->order('create_time desc')->select();
        $this->assign('link',$link);
        $advs = M('Advertisement')->where(array('show' => '1'))->order('create_time desc')->select();
        $this->assign('advs',$advs);
        //        微博
        $weibo = M('Url')->find();
        $this->assign('weibo', $weibo);
        $this->assign('server', $server);
        $this->assign('list', $list);
        $this->assign('page', $page->showFront());
        $this->display();
    }

    public function detail($id)
    {
        if ($id == null) {
            $this->error('文档id错误');
        } else {
            //        微博
            $weibo = M('Url')->find();
            $this->assign('weibo', $weibo);
            $data = D('MainInfo')->where(array('id' => $id))->find();
            $content = D('InfoDetail')->where(array('info_id' => $id))->field('message')->find();
            $advs = M('Advertisement')->where(array('show' => '1'))->order('create_time desc')->select();
            /** 友情链接调用**/
            $link = D('Link')->order('create_time desc')->select();
            $this->assign('link',$link);
            $this->assign('advs',$advs);
            $this->assign('data', $data);
            $this->assign('content', $content);
            $this->display();
        }
    }

    public function best()
    {
        /** 客服 **/
        $server = D('Server')->limit(1)->select();
        $this->assign('server', $server);

        //        微博
        $weibo = M('Url')->find();
        $this->assign('weibo', $weibo);

        /** 友情链接调用**/
        $link = D('Link')->order('create_time desc')->select();
        $this->assign('link', $link);

        /** 广告 **/
        $advs = M('Advertisement')->where(array('show' => '1'))->order('create_time desc')->select();
        $this->assign('advs', $advs);

        $list = M('Main_info')->where(array('show' => 5, 'finish'=> 1))->order('modify_time desc')->select();
        $this->assign('list',$list);
        $id = I('id','');
        if ($id != '') {
            $data =  M('Main_info')->where(array('id' => $id,'show' => 5, 'finish'=> 1))->find();
            $content =  M('Main_info')->where(array('info_id' => $id))->field('message')->find();
            $this->assign('data', $data);
            $this->assign('content', $content);
        }else{
            $id = $list[0]['id'];
            $data =  M('Main_info')->where(array('id' => $id,'show' => 5, 'finish'=> 1))->find();
            $content =  M('Main_info')->where(array('info_id' => $id))->field('message')->find();
            $this->assign('data', $data);
            $this->assign('content', $content);
        }
        $this->display();
    }
}